﻿//http://www.makebarcode.com/specs/ean_cc.html
var BarCodeType = new Array(
    "Undefined",
        /// <summary>Mã Coupon</summary>
        "MC",
        /// <summary>Mã Coupon phieu</summary>
        "MS"
);

function parseBarcode(barCodeString) {
    
   var type, number;
    if (barCodeString.length == 12)
        barCodeString = '0' + barCodeString;
    // long format
    if (barCodeString.length == 13) {
        type = BarCodeType[new Number(barCodeString.substr(0, 2))]; // 2 so dau la index trong array
        number = new Number(barCodeString.substr(2, 10)); // 10 so tiep theo la so id
     
        return { Type: type, Number: number, BarCode: barCodeString };
    } else { // short format
        type = barCodeString.toUpperCase().substr(0, 2); // 2 so dau la ma barcode
        number = new Number(barCodeString.substr(2, 10)); // 10 so tiep theo la so id
        return { Type: type, Number: number, BarCode: barCodeString };
    }
}

function parseVoucherNumber(voucherNumber) {
    var floatingNumber = Math.pow(10, 4);
    return { SKUID: ~~(voucherNumber / floatingNumber), SerialNumber: voucherNumber % floatingNumber };
}

$(function () {
    var hiddenStyle = "";

    // neu trien khai live (co dau cham phan cach)
    if (window.location.href.indexOf('.') > 0)
        hiddenStyle = "left: -100px; position: absolute; z-index: -10;";
    
    var html = '<li style="margin-top:-10px">' +
        '<span id="barcodeLabel" style="display: none">Đang bật chế độ quét barcode</span><button id="btnStartScan" class="btn_scan"><img width="30" height="18" class="icon_btn" src="/Content/Images/scan2.png"> Bắt đầu Scan</button><input type="text" id="barcodeInput" class="barcodeInput" style="' + hiddenStyle + '"/>' +
        '</br><label id="lbBarcode"></label>' +
        '</li>';
    if ($("ul.list_right_btn").length == 0) {
        html = '<ul class="list_right_btn">' + html + '</ul>';
        $('div.ad_title_bg:first').append(html);
    } else {
        $('ul.list_right_btn:first').append(html);
    }

    $('#btnStartScan').click(function () {
        $('.barcodeInput').focus();
        return false; // prevent submit on enter
    });

    $(".barcodeInput").keydown(function (e) {
        var key = e.charCode || e.keyCode || 0;
        if (key == 13) {
            var barcodeString = $(this).val();
            var barcode = parseBarcode(barcodeString);
            $("#lbBarcode").html(barcode.BarCode);
            if ($.isFunction(window.ProcessBarcode) || $.isFunction(document.ProcessBarcode)) {
                ProcessBarcode(barcode);
                $(".barcodeInput").val("");
            }
        }
    });

    $('.barcodeInput').blur(function () {
        $('#btnStartScan').show();
        $('#barcodeLabel').hide();
    }).focusin(function () {
        $('#btnStartScan').hide();
        $('#barcodeLabel').show();
    });

    // xu ly enter va focus vao hidden 
    $('.Scan input[type="text"]').keydown(function (e) {
        var key = e.charCode || e.keyCode || 0;
        if (key == 13) {
            $(".barcodeInput").focus();
        }
    });

    $('html').bind('click', function () {

        // khong co focus vao element
        if ($('.not-focus').is(":focus")) {
            $(".barcodeInput").focus();
            return false;
        }

        // click tren cac element input, cho phep focus
        if ($('select, input, textarea').is(":focus")) {
            return true;
        }

        // click o ngoai trang -> focus vao barcode input
        $(".barcodeInput").focus();

    });
});